home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: What`s better & why
- Date: 22 Feb 1996 06:47:05 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4gh3h9$d1b@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
-
- [Correction to earlier post included below (sorry)]
- Scott Hawley <shawley@connix.com>
- in <31297C5A.E6C@connix.com> asks:
-
- >I was just curious what you all though about the following code.
- >Please tell me what is better (faster/Smaller) and why. Or does it make
- >any difference at all. I just though about this while I was programming
- >today?
-
- >example 1:
- > val = 1;
- > if( what ever...)val = 0;
-
- >or
- >example 2:
- > if( what ever... )val = 0;
- > else val = 1;
-
- It all depends on your compiler. With mine, there's no difference.
- Below notice the side-by-side comparison of three [now 4] functions and
- the code the compiler generates:
-
- int func1(int x) | int func2(int x) | int func3(int x)
- { | { | {
- int val; | int val; | int val;
- val = 1; | if (x > 0) | val = x <= 0;
- if (x > 0) | val = 0; | return val;
- val = 0; | else | }
- return val; | val = 1; |
- | return val; /* omitted in earlier post */
- | } |
-
- .globl _func1 | .globl _func2 | .globl _func3
- _func1: | _func2: | _func3:
- pushl %ebp | pushl %ebp | pushl %ebp
- movl %esp,%ebp | movl %esp,%ebp | movl %esp,%ebp
- cmpl $0,8(%ebp) | cmpl $0,8(%ebp) | cmpl $0,8(%ebp)
- setle %al | setle %al | setle %al
- andl $255,%eax | andl $255,%eax | andl $255,%eax
- leave | leave | leave
- ret | ret | ret
- | |
-
- [and here's the new fourth one, also the same code out]
- int func4(int x) .globl _func4
- { _func4:
- return x <= 0; pushl %ebp
- } movl %esp,%ebp
- cmpl $0,8(%ebp)
- setle %al
- andl $255,%eax
- leave
- ret
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-